import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:jtsell_app/entity/goods_detail_entity.dart';
import 'package:jtsell_app/entity/user_login_entity.dart';
import 'package:jtsell_app/entity/wechat_pay_entity.dart';
import '../entity/mall_goods_entity.dart';
import '../entity/category_entity.dart';
import '../exception/app_exception.dart';
import '../config/app_config.dart';
import '../network/http_utils.dart';
import '../entity/slide_entity.dart';

import 'package:fluttertoast/fluttertoast.dart';

class HttpDispense {
  _HttpDispense() {}

  ///
  /// 弹出错误信息
  ///
  static void showErrorToast([String errMsg = "操作失败,请稍后重试"]) {
    Fluttertoast.showToast(
        msg: errMsg,
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.BOTTOM,
        timeInSecForIos: 1,
        backgroundColor: AppConfig.TOAST_BACKGROUND_COLOR,
        textColor: AppConfig.TOAST_TEXT_COLOR,
        fontSize: ScreenUtil().setSp(45));
  }

  ///
  /// 获得商城首页轮播图
  ///
  static Future<SlideEntity> getSlidesContent() async {
//    final AsyncMemoizer _memoizer = AsyncMemoizer();
//
//    return _memoizer.runOnce(() async {
//      ///Memoization缓存函数的返回值，并在再次调用该函数时重用它
//    });

    return await HttpUtils<SlideEntity>().getParseHttpResult(HttpUrls.SLIDES_URL).then(
        (httpResult) {
      return httpResult.data;
    }, onError: (error) {
      AppException appException = error as AppException;
      showErrorToast(appException.message);
      throw error;
    });
  }

  ///
  /// 获得商城首页分类信息
  ///
  static Future<List<CategoryEntity>> getCategoryList() async {
    return await HttpUtils<dynamic>().getParseHttpResult(HttpUrls.CATEGORY_URL).then((httpResult) {
      List<CategoryEntity> categoryList = List();

      List tempList = httpResult.data as List;
      tempList.forEach((categoryJson) {
        CategoryEntity categoryEntity = CategoryEntity.fromJson(categoryJson);
        print(categoryEntity);

        categoryList.add(categoryEntity);
      });

      return categoryList;
    }).catchError((error) {
      AppException appException = error as AppException;
      showErrorToast(appException.message);
      throw error;
    });
  }

  ///
  /// 获取热门商品
  ///
  static Future<List<MallGoodsEntity>> getMallGoodsList(Map<String, dynamic> params) async {
    return await HttpUtils<dynamic>()
        .postParseHttpResult(HttpUrls.GET_MALL_GOODS, queryParameters: params)
        .then((httpResult) {
      List<MallGoodsEntity> mallGoodsList = List();

      List tempList = httpResult.data as List;

      tempList.forEach((categoryJson) {
        MallGoodsEntity mallGoodsEntity = MallGoodsEntity.fromJson(categoryJson);

        mallGoodsList.add(mallGoodsEntity);
      });

      return mallGoodsList;
    }).catchError((error) {
      AppException appException = error as AppException;
      showErrorToast(appException.message);
      throw error;
    });
  }

  ///
  /// 根据类别获取商品信息
  ///
  static Future<List<MallGoodsEntity>> getMallGoodsListByCategory(Map<String, dynamic> data) async {
    return await HttpUtils<dynamic>()
        .postParseHttpResult(HttpUrls.GET_GOODS_BY_CATEGORY, queryParameters: data)
        .then((httpResult) {
      List<MallGoodsEntity> mallGoodsList = List();

//      throw AppException(message: "测试错误");

      List tempList = httpResult.data as List;
      if (tempList == null) {
        return mallGoodsList;
      }

      tempList.forEach((categoryJson) {
        MallGoodsEntity mallGoodsEntity = MallGoodsEntity.fromJson(categoryJson);

        mallGoodsList.add(mallGoodsEntity);
      });

      return mallGoodsList;
    }).catchError((error) {
      if (error is AppException) {
        showErrorToast(error.message);
      }
      throw error;
    });
  }

  ///
  /// 获取热门商品
  ///
  static Future<GoodsDetailEntity> getGoodDetailById(String goodsID) async {
    return await HttpUtils<GoodsDetailEntity>().postParseHttpResult(HttpUrls.GET_GOODS_BY_ID,
        queryParameters: {"goodId": goodsID}).then((httpResult) {
      return httpResult.data;
    }).catchError((error) {
      if (error is AppException) {
        showErrorToast(error.message);
      }
      throw error;
    });
  }

  ///
  /// 获取微信支付
  ///
  static Future<WechatPayEntity> getWechatInfo() async {
    return await HttpUtils<WechatPayEntity>()
        .postParseEntity(
      HttpUrls.WECHAT_PAY_INFO,
    )
        .then((wxPay) {
      return wxPay;
    }).catchError((error) {
      if (error is AppException) {
        showErrorToast(error.message);
      }
      throw error;
    });
  }

  ///
  /// 微信登录
  ///
  static Future<UserLoginEntity> wechatLogin(String wechatCode) async {
    return await HttpUtils<UserLoginEntity>().postParseHttpResult(HttpUrls.WECHAT_LOGIN,
        queryParameters: {"code": wechatCode}).then((wxPay) {


      return wxPay.data;


    }).catchError((error) {
      if (error is AppException) {
        showErrorToast(error.message);
      }
      throw error;
    });
  }
}
